﻿using FX_API.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static FX_API.Delegates;

namespace FX_API.Request
{
    /// <summary>
    /// 带从对象的纷享对象上传模板
    /// </summary>
    /// <typeparam name="FX_MODEL"></typeparam>
    /// <typeparam name="FX_DETAIL"></typeparam>
    public abstract class FXBaseUploadService<FX_MODEL, FX_DETAIL> where FX_MODEL : IObjectData
    {
        public HandlerFunc InfoFunc { protected get; set; }
        public HandlerFunc ErrFunc { protected get; set; }
        public HandlerFunc WarningFunc { protected get; set; }

        protected readonly FXCommonUploadRequest request;

        protected FXBaseUploadService()
        {
            request = new FXCommonUploadRequest
            {
                exceptionHandler = (Exception ex) =>
                {
                    string reason = $"执行{GetType().Name}时发生程序错误;";
                    string content = $"错误信息:{ex.Message},错误堆栈:{ex.StackTrace}";
                    ErrFunc?.Invoke(reason, content);
                },
                httpErrHandler = (string responseMsg, FXCreateRequestModel request) =>
                {
                    string reason = $"执行{GetType().Name}时发生请求错误;";
                    string content = $"返回信息:{responseMsg},请求体:{request}";
                    ErrFunc?.Invoke(reason, content);
                }
            };
        }

        public FXCreateResponseModel Upload(FX_MODEL objectData, FX_DETAIL details, bool triggerWorkFlow = false)
        {
            return request.Upload(objectData, details, triggerWorkFlow);
        }
    }

    /// <summary>
    /// 不带从对象的纷享对象上传模板
    /// </summary>
    /// <typeparam name="FX_MODEL"></typeparam>
    public abstract class FXBaseUploadService<FX_MODEL> : FXBaseUploadService<FX_MODEL, object> where FX_MODEL : IObjectData
    {
        public FXCreateResponseModel Upload(FX_MODEL objectData, bool triggerWorkFlow = false)
        {
            return request.Upload(objectData, null, triggerWorkFlow);
        }
    }
}
